﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Pampa.CRUD.Configuration;
using Pampa.CRUD.Configuration.Validations;
using Pampa.MVC.Fluent.Crud.Groups;

namespace Pampa.MVC.Fluent.Crud.Items
{
    public class EditItemBuilder<TGroup, TEditor, TEntity, TThis> : VisibleItemBuilder<TGroup, TEditor, TEntity, TThis> where TThis : EditItemBuilder<TGroup, TEditor, TEntity, TThis>
    {
        private readonly EditItem _editItem;

        public EditItemBuilder(EditItem editItem) : base(editItem)
        {
            _editItem = editItem;
        }

        public EditItemBuilder(GroupBuilder<TGroup, TEditor> group, EditItem editItem) : base(group, editItem)
        {
            _editItem = editItem;
        }

        public TThis Required(string errorMessage = null)
        {
            var error = errorMessage ?? string.Format("{0} is required.", _editItem.Label);
            var validation = new RequiredValidation(error);
            _editItem.Validations.Add(validation);
            return (TThis)this;
        }

        public TThis Integer(string errorMessage = null, int? minValue = null, int? maxValue = null)
        {
            var error = errorMessage ?? string.Format("{0} is invalid.", _editItem.Label);
            var validation = new RangeValidation();
            validation.DataType = "Integer";
            validation.MinValue = minValue != null ? minValue.ToString() : "";
            validation.MaxValue = maxValue != null ? maxValue.ToString() : "";
            validation.LessOrEqual = true;
            validation.GratherOrEqual = true;
            validation.ErrorMessage = error;
            _editItem.Validations.Add(validation);
            return (TThis)this;
        }

        public TThis Decimal(string errorMessage = null, decimal? minValue = null, decimal? maxValue = null)
        {
            var error = errorMessage ?? string.Format("{0} is invalid.", _editItem.Label);
            var validation = new RangeValidation();
            validation.DataType = "Decimal";
            validation.MinValue = minValue != null ? minValue.ToString() : "";
            validation.MaxValue = maxValue != null ? maxValue.ToString() : "";
            validation.LessOrEqual = true;
            validation.GratherOrEqual = true;
            validation.ErrorMessage = error;
            _editItem.Validations.Add(validation);
            return (TThis)this;
        }

        public TThis Date(string errorMessage = null, string format = "dmy", DateTime? minValue = null, DateTime? maxValue = null)
        {
            var error = errorMessage ?? string.Format("{0} is invalid.", _editItem.Label);
            var validation = new RangeValidation();
            validation.DataType = "Date{"+ format + "}";
            validation.MinValue = minValue != null ? minValue.ToString() : "";
            validation.MaxValue = maxValue != null ? maxValue.ToString() : "";
            validation.LessOrEqual = true;
            validation.GratherOrEqual = true;
            validation.ErrorMessage = error;
            _editItem.Validations.Add(validation);
            return (TThis)this;
       }

        public TThis Email(string errorMessage = null)
        {
            var error = errorMessage ?? string.Format("{0} is invalid.", _editItem.Label);
            var validation = new PatternValidation(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", error);
            _editItem.Validations.Add(validation);
            return (TThis)this;
        }

        public TThis Pattern(string regex, string errorMessage = null)
        {
            var error = errorMessage ?? string.Format("{0} is invalid.", _editItem.Label);
            var validation = new PatternValidation(regex, error);
            _editItem.Validations.Add(validation);
            return (TThis)this;
        }
    }
}
